/*
 * CS_REQ_WORLD_BOSS_PARTY_LIST.cpp
 *
 *  Created on: 2014. 1. 8.
 *      Author: jjaehuny
 */



#include "../../Command.h"

#include "../../User.h"
#include "../../NLogic/Space.h"
#include "../../NLogic/Static.h"

namespace ProjectZ { namespace NCommand {

static void SendAck_CS_REQ_WORLD_BOSS_PARTY_LIST(User * pUser, int16_t ack)
{
	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ack);
	pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_PARTY_LIST);
	pUser->Send(pAck);
}

void Command::CS_REQ_WORLD_BOSS_PARTY_LIST(User * pUser, ::Network::Packet * pPacket)
{
	/*
	/// 월드 보스 방만들기
	CS_REQ_WORLD_BOSS_PARTY_LIST 		0x4002
	{
	}

	SC_REQ_WORLD_BOSS_PARTY_LIST 		0x4003
	{
		s16 sAck;						/// 생성된 파티 없음
		u16 count 						/// 조인 가능한 파티 리스트
		{
			u32 u32room_number;			/// 방번호 고유값 파티 시퀀스와 같으면 별도로 필요하지 않음
			u8 u8party_member_num;		/// 모집하는 파티원 수 1 or 2
			u8 count					/// 파티원 수만큼
			{
				u8 u8class_type;
				u16 u16lv;
			}
		}
	}
	*/

	NLogic::Space * pWorld = pUser->GetSpace();
	if (NULL == pWorld)
	{
		DEBUGLOG("%s NULL == pWorld", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_PARTY_LIST(pUser, ACK_NO_SPACE);
		return;
	}

	if (pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD)
	{
		DEBUGLOG("%s pWorld->GetData()._spaceType != NLogic::Space::SpaceType::WORLD", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_PARTY_LIST(pUser, ACK_NO_SPACE);
		return;
	}

	if (pUser->GetEnumState() != User::EnumState::__NONE__)
	{
		DEBUGLOG("%s pUser->GetEnumState() != User::EnumState::__NONE__", __FUNCTION__);
		SendAck_CS_REQ_WORLD_BOSS_PARTY_LIST(pUser, ACK_UNKNOWN_ERROR);
		return;
	}

	NLogic::Static::WORLDBOSSROOMTREE & worldBossRoomTree = NLogic::Static::Instance().GETWORLDBOSSROOMTREE();
	if (worldBossRoomTree.size() <= 0)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		* pAck << static_cast<int16_t>(ACK_OK);
		* pAck << static_cast<uint16_t>(0);
		pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_PARTY_LIST);
		pUser->Send(pAck);
		return;
	}

	std::vector<NLogic::Space * > clsSpaceVector;
	auto spaceItr = worldBossRoomTree.begin();
	for ( ;spaceItr != worldBossRoomTree.end(); ++spaceItr)
	{
		NLogic::Space * pSpace = spaceItr->second;
		NLogic::IndexerWorldBoss * pIndexer = pSpace->GetIndexerWorldBoss();
		if (NULL == pIndexer)
			continue;

		NLogic::Party * pParty = pSpace->GetParty();
		if (NULL == pParty)
			continue;

		if (static_cast<int>(pParty->GetAllUserTree().size()) >= pSpace->GetData()._maxUser + 1)
			continue;

		if (pParty->GetState() != NLogic::Party::State::WORLD_BOSS_WAIT)
			continue;

		clsSpaceVector.push_back(pSpace);
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	* pAck << static_cast<int16_t>(ACK_OK);
	* pAck << static_cast<uint16_t>(clsSpaceVector.size());
	DEBUGLOG("ROOM COUNT : %d", clsSpaceVector.size());

	auto openSpaceItr = clsSpaceVector.begin();
	for ( ;openSpaceItr != clsSpaceVector.end(); ++openSpaceItr)
	{
		NLogic::Space * pOpenSpace = ( * openSpaceItr );
		NLogic::IndexerWorldBoss * pIndexer = pOpenSpace->GetIndexerWorldBoss();
		NLogic::Party * pParty = pOpenSpace->GetParty();

		uint32_t u32room_number = pIndexer->GetIndex();
		uint8_t u8party_member_num = static_cast<uint8_t>(pOpenSpace->GetData()._maxUser);

		DEBUGLOG("u32room_number : %d, u8party_member_num : %d", u32room_number, u8party_member_num);

		auto partyAllUserTree = pParty->GetAllUserTree();
		uint8_t count = partyAllUserTree.size();

		* pAck << u32room_number << u8party_member_num << count;

		DEBUGLOG("userCount : %d", count);

		std::for_each(partyAllUserTree.begin(), partyAllUserTree.end(), [&](NLogic::Party::USERTREE::value_type & pair) {
			User * pUser = pair.second;

			uint8_t u8class_type = static_cast<uint8_t>(pUser->GetCharacterInfo().classtype);
			uint16_t u16lv = static_cast<uint16_t>(pUser->GetCharacterInfo().level);

			* pAck << u8class_type << u16lv;

			DEBUGLOG("u8class_type : %d, u16lv : %d", u8class_type, u16lv);
		} );
	}

	pAck->MakePacket(CMD_SC_REQ_WORLD_BOSS_PARTY_LIST);
	pUser->Send(pAck);
	clsSpaceVector.clear();
}

} /*NCommand*/ } /*ProjectZ*/
